package com.yinda.dt.produce.module.order;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xcgame.srv.SuperRecyclerView;
import com.yinda.dt.produce.R;
import com.yinda.dt.produce.app.DemoActivity;
import com.yinda.dt.produce.app.DemoConfig;
import com.yinda.dt.produce.app.MyResponseCallback;
import com.yinda.dt.produce.module.login.LoginActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class OrderClassifyActivity extends DemoActivity implements View.OnClickListener, SuperRecyclerView.OnPullRefreshListener {

    private LinearLayout ll_left;
    private SuperRecyclerView srv_content;
    private OrderClassifyAdapter adapter;
    private JSONArray jsonOrderClassifys;

    private boolean isWait;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_classify);

        isWait = getIntent().getBooleanExtra("isWait", false);

        ll_left = (LinearLayout) findViewById(R.id.ll_left);
        ll_left.setOnClickListener(this);
        srv_content = (SuperRecyclerView) findViewById(R.id.srv_content);
        srv_content.setRefreshProgressColorsRes(R.color.colorPrimary);
        srv_content.setPullRefreshEnabled(true);
        srv_content.setOnPullRefreshListener(this);
        srv_content.setLoadMoreEnabled(false);
        srv_content.setEmptyView(R.layout.view_no_date);

        adapter = new OrderClassifyAdapter();
        srv_content.setAdapter(adapter);
        srv_content.setRefreshing();

        getOrderTypeList();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ll_left) {
            finish();
        }
    }

    @Override
    public void onPullRefresh() {
        getOrderTypeList();
    }

    private void getOrderTypeList() {
        try {
            String url = getString(R.string.defaultURL) + "/phone1_0!OrderTypeList.action";
            JSONObject jsonParams = new JSONObject();
            jsonParams.put("status", isWait);
            post(url, jsonParams, new MyResponseCallback() {

                @Override
                public void onFinish() {
                    super.onFinish();
                    srv_content.refreshComplete();
                }

                @Override
                public void onSuccess(JSONObject jsonResult) {
                    super.onSuccess(jsonResult);

                    try {
                        JSONObject jsonHead = jsonResult.getJSONObject("head");
                        int status = jsonHead.getInt("status");
                        String info = jsonHead.getString("info");
                        if (status == 1) {

                            jsonOrderClassifys = jsonResult.getJSONArray("data");
                            adapter.notifyDataSetChanged();

                        } else {
                            if (info.contains("key无效") || info.toLowerCase().contains("loginout") || info.contains("未登录")) {
                                DemoConfig.getInstance(getContext()).setKey("");
                                DemoConfig.getInstance(getContext()).setIsLogin(false);
                                showToast("请重新登录");
                                Intent intent = new Intent(getContext(), LoginActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);
                            } else {
                                showToast(info);
                            }
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private class OrderClassifyAdapter extends RecyclerView.Adapter<OrderClassifyAdapter.ViewHolder> {

        class ViewHolder extends RecyclerView.ViewHolder {

            LinearLayout ll_item;
            TextView tv_number;
            TextView tv_name;
            TextView tv_count;

            public ViewHolder(View itemView) {
                super(itemView);
                ll_item = (LinearLayout) itemView.findViewById(R.id.ll_item);
                tv_number = (TextView) itemView.findViewById(R.id.tv_number);
                tv_name = (TextView) itemView.findViewById(R.id.tv_name);
                tv_count = (TextView) itemView.findViewById(R.id.tv_count);
            }
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(OrderClassifyActivity.this).inflate(R.layout.adapter_order_classify, parent, false);
            return new ViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            try {
                final JSONObject jsonOrderClassify = jsonOrderClassifys.getJSONObject(position);

                holder.tv_number.setText(position + 1 + "");
                holder.tv_name.setText(jsonOrderClassify.getString("title"));
                holder.tv_count.setText(jsonOrderClassify.getString("count"));

                holder.ll_item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        try {

                            if (isWait) {

                                if (jsonOrderClassify.getString("title").equals("技术资料")) {

                                    Intent intent = new Intent(OrderClassifyActivity.this, NewWaitOrderListActivity.class);
                                    intent.putExtra("approvalCode", jsonOrderClassify.getString("approvalCode"));
                                    intent.putExtra("type", 1);
                                    startActivity(intent);

                                } else if (jsonOrderClassify.getString("title").equals("半成品")) {
                                    Intent intent = new Intent(OrderClassifyActivity.this, NewWaitOrderListActivity.class);
                                    intent.putExtra("approvalCode", jsonOrderClassify.getString("approvalCode"));
                                    intent.putExtra("type", 2);
                                    startActivity(intent);
                                }else {
                                    Intent intent = new Intent(OrderClassifyActivity.this, WaitOrderListActivity.class);
                                    intent.putExtra("approvalCode", jsonOrderClassify.getString("approvalCode"));
                                    startActivity(intent);
                                }

                            } else {
                                Intent intent = new Intent(OrderClassifyActivity.this, OrderListActivity.class);
                                intent.putExtra("approvalCode", jsonOrderClassify.getString("approvalCode"));
                                startActivity(intent);
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }
                });

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public int getItemCount() {
            if (jsonOrderClassifys == null) {
                return 0;
            }
            return jsonOrderClassifys.length();
        }
    }
}
